package easy;

import java.util.HashSet;
import java.util.Set;

public class Solution_653 {

    private boolean flag = false;
    private Set<Integer> set = new HashSet<>();

    public boolean findTarget(TreeNode root, int k) {
        dps(root, k);
        return flag;
    }

    public void dps(TreeNode node, int k) {
        if (node == null || flag) {
            return;
        }
        int sub = k - node.val;
        if (set.contains(sub)) {
            flag = true;
            return;
        }
        set.add(node.val);
        dps(node.left, k);
        dps(node.right, k);
    }

    public static void main(String[] args) {
        Solution_653 model = new Solution_653();
        Solution_653.TreeNode root = new Solution_653.TreeNode(5);
        root.left = new Solution_653.TreeNode(3);
        root.left.left = new Solution_653.TreeNode(2);
        root.left.right = new Solution_653.TreeNode(4);

        root.right = new Solution_653.TreeNode(6);
//        root.right.left = new Solution_543.TreeNode(5);
        root.right.right = new Solution_653.TreeNode(7);
        System.out.println(model.findTarget(root, 13));
    }

    public static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
